package com.vercon.otamirror.fragment;

import static com.vercon.otamirror.MainActivity.SCREEN_OFF;
import static com.vercon.otamirror.MainApplication.policyManager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.abupdate.iot_libs.OtaAgentPolicy;
import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.abupdate.iot_libs.interact.callback.ICheckVersionCallback;
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.vercon.commonmirror.Constants;
import com.vercon.commonmirror.apng.APNGDrawable;
import com.vercon.commonmirror.cache.MirrorDataStorage;
import com.vercon.commonmirror.presenter.MirrorResultView;
import com.vercon.commonmirror.utils.APPUtil;
import com.vercon.commonmirror.utils.LogUtils;
import com.vercon.otamirror.MainApplication;
import com.vercon.otamirror.R;
import com.vercon.otamirror.mode.FirmwareMode;
import com.vercon.otamirror.utils.helper.AppUtil;


import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by csh on 2021/1/22
 * 首页fragment
 */
public class HomeFragment extends BaseFragment  {


    private final String TAG = getClass().getSimpleName();

    @Override
    protected int setContentView(@Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home; //K系列
    }



    @Override
    protected void initView(View view) {
        LogUtils.e(TAG, "initView");

//        tv_temperature.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
//                APPUtil.addPackageName("com.android.settings.Settings");
//
//                getVersion();
//            }
//        });
    }

    @Override
    protected void initListener(View view) {

    }

    private void getVersion() {

        /**
         * 检测版本
         * 异步回调请求方法，可以在子线程中调用，也可在主线程中调用，回调方法在主线程，便于UI处理。
         */
        OtaAgentPolicy.checkVersion().enqueue(new ICheckVersionCallback() {
            @Override
            public void onCheckSuccess(List<VersionInfo> infoList) {
                //发现有新版本
                LogUtils.d("FotaApplication 发现有新版本 " + infoList.size());
                for (VersionInfo info : infoList) {
                    LogUtils.d("FotaApplication 版本 " + info.toString());
                }
                try {
                    VersionInfo  versionInfo =  infoList.get(0);
                    LogUtils.d("FotaApplication 发现有新版本 " + infoList.size());
                    if (policyManager != null) {
                        FirmwareMode mode = new FirmwareMode(APPUtil.getFirmwareUpdateVersion_k(versionInfo.versionName),
                                APPUtil.getFirmwareUpdateVersion_k(versionInfo.oldVersionName), versionInfo.deltaUrl,
                                AppUtil.getContent(versionInfo.content), true, versionInfo.publishDate, true);
                        LogUtils.i("FotaApplication", "33");
                        EventBus.getDefault().post(mode);
                    }else {
                        LogUtils.i("FotaApplication", "policyManager为空");
                    }
                }catch (Exception e){
                    LogUtils.i("FotaApplication", "Exception="+e.getMessage());
                }


            }

            @Override
            public void onCheckFail(int errorCode) {
                //没有新版本

                LogUtils.d("FotaApplication 没有新版本 " + errorCode);

            }
        });
    }


}